package com.yisu.expressway.ui.recyclerview;

import android.support.annotation.CallSuper;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;

/**
 * Created by will on 16/3/9.
 */
public abstract class BaseRvAdapter <T, VH extends BaseRvAdapter.SampleViewHolder>
        extends RecyclerView.Adapter<VH> {

    @CallSuper
    @Override public void onViewRecycled(VH holder) {
        holder.onRecycled();
    }

    @CallSuper @Override public boolean onFailedToRecycleView(VH holder) {
        return holder.onFailedToRecycle();
    }

    @CallSuper @Override public void onViewAttachedToWindow(VH holder) {
        holder.onAttachedToParent();
    }

    @CallSuper @Override public void onViewDetachedFromWindow(VH holder) {
        holder.onDetachedFromParent();
    }

//  @CallSuper @Override public void onBindViewHolder(VH holder, int position) {
////    holder.bind(getItem(position));
//    onBindItemViewHolder(holder, position);
//    holder.onViewHolderBound();
//  }
//
//  /**
//   * Require client to feed data. Actually object returned from this method could be null.
//   */
//  @Nullable protected abstract T getItem(int position);
//  @Nullable public abstract void onBindItemViewHolder(VH holder, int position);


    public static class SampleViewHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> views = new SparseArray<View>();

        @SuppressWarnings("unchecked")
        public <T extends View> T getView(int resId) {
            View v = views.get(resId);
            if (null == v) {
                v = itemView.findViewById(resId);
                views.put(resId, v);
            }
            return (T) v;
        }

        public SampleViewHolder(View itemView) {
            super(itemView);
        }


        /**
         * Client can setup long click listener from inside viewHolder
         */
        public void setOnItemLongClickListener(View.OnLongClickListener listener) {
            itemView.setOnLongClickListener(listener);
        }

        /**
         * Called by {@link RecyclerView.Adapter#onViewAttachedToWindow(RecyclerView.ViewHolder)}
         */
        @CallSuper public void onAttachedToParent() {

        }

        /**
         * Called by {@link RecyclerView.Adapter#onDetachedFromRecyclerView(RecyclerView)}
         */
        @CallSuper public void onDetachedFromParent() {

        }

        /**
         * Called by {@link RecyclerView.Adapter#onViewRecycled(RecyclerView.ViewHolder)}
         */
        public void onRecycled() {

        }

        /**
         * Called by {@link RecyclerView.Adapter#onFailedToRecycleView(RecyclerView.ViewHolder)}
         */
        public boolean onFailedToRecycle() {
            return false;
        }

        /**
         * Called response to {@link RecyclerView.Adapter#onBindViewHolder(RecyclerView.ViewHolder,
         * int)}
         */
        public void onViewHolderBound() {

        }

        /**
         * Accept null object, but client must acknowledge this, and try to supply valid object to
         * ViewHolder
         */
//    public abstract void bind(@Nullable Object object);

    }
}
